package com.lkm.app_ui.ui.second;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lkm.app_ui.MBaseFragment;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.m.fun.OnSaleManifestSyncEvent;
import com.lkm.app_ui.R;
import com.lkm.app_ui.m.fun.SaleManifestEntity;
import com.lkm.comlib.help.ViewHelp;
import com.lkm.comlib.ui.BaseHoldAdapter;
import com.lkm.comlib.ui.IHoldView;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.Observer;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.NumberHelp;
import com.lkm.market2_0.help.SteelardHelp;
import com.lkm.market2_0.m.steelyarddata.OrderEntity;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;




public class SaleingFragment extends MBaseFragment {

    public static SaleingFragment getInstance() {
        SaleingFragment f = new SaleingFragment();
        Bundle data = new Bundle();
        f.setArguments(data);
        return f;
    }

    @Override
    protected boolean configIsLockKey() {
        return false;
    }

    @Override
    protected int configLayoutResID() {
        return R.layout.fragment_sec_saleing;
    }

    private final List<OrderEntity.GoodEntity> souceList =new ArrayList<>();
    private MAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinkBus.registerReceiver(onSaleManifestSyncEventObserver);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ListView listView=(ListView)findViewById(android.R.id.list);
        listView.setAdapter(adapter=new MAdapter());

        ((TextView)findViewById(R.id.tv_weight_unit)).setText("重量("+ RunTimeData.getAppWeightUnit()+")");
    }

    @Override
    public void onDestroy() {
        LinkBus.unregisterReceiver(onSaleManifestSyncEventObserver);
        super.onDestroy();

    }

    private Observer<OnSaleManifestSyncEvent> onSaleManifestSyncEventObserver = new Observer<OnSaleManifestSyncEvent>() {
        @Override
        public Class getBusEventClass() {
            return OnSaleManifestSyncEvent.class;
        }

        @Override
        public void onReceiver(OnSaleManifestSyncEvent data, Runnable nextRun) {

            try{
                binData(data.extSaleManifestEntity);
            }finally {
                nextRun.run();
            }
        }
    };

    private void binData(SaleManifestEntity saleManifestEntity){

        ((TextView)(findViewById(R.id.tv_discount))).setText(
                Html.fromHtml("优惠折扣：<font color='#ff0000'>" + saleManifestEntity.getDiscount() + "</font>"));
        ((TextView)(findViewById(R.id.tv_discountAmount))).setText(
                Html.fromHtml("优惠金额：<font color='#ff0000'>"+saleManifestEntity.getDiscountAmount()+"</font>"));

        ((TextView)(findViewById(R.id.tv_totalAmount))).setText(
                Html.fromHtml("消费金额：<font color='#ff0000'>"+ saleManifestEntity.getTotalAmount()+"</font>"));
        ((TextView)(findViewById(R.id.tv_payableAmount))).setText(
                Html.fromHtml("应付金额：<font color='#ff0000'>"+saleManifestEntity.getPayableAmount()+"</font>"));


        souceList.clear();
        List<OrderEntity.GoodEntity> soucel=saleManifestEntity.getSouceList();
        if(soucel!=null)
            souceList.addAll(soucel);

        adapter.notifyDataSetChanged();
    }




    private class MAdapter extends BaseHoldAdapter {
        public MAdapter() {
            super(holdCycleHelp());
        }

        @Override
        public IHoldView createHoldView(int position, View convertView, ViewGroup parent) {
            return new MHoldView();
        }

        @Override
        public int getCount() {
            return CollectionHelp.getSize(souceList);
        }

        @Override
        public Object getItem(int position) {
            return souceList.get(position);
        }
    }
    private class MHoldView extends LinearLayout implements IHoldView<OrderEntity.GoodEntity>  {

        private TextView tv_name,tv_price_unit,tv_weight,tv_totalPrice;
        public MHoldView() {
            super(getActivity());
            ViewHelp.inflate(getActivity(), R.layout.item_sec_saleing, this);
            tv_name=(TextView)this.findViewById(R.id.tv_name);
            tv_price_unit=(TextView)this.findViewById(R.id.tv_price_unit);
            tv_weight=(TextView)this.findViewById(R.id.tv_weight);
            tv_totalPrice=(TextView)this.findViewById(R.id.tv_totalPrice);

        }

        private int position;


        @Override
        public Object binData(OrderEntity.GoodEntity obj, int position, boolean isonPause) {
            this.position=position;

            tv_name.setText(obj.goodName);
            tv_price_unit.setText(SteelardHelp.formatMoney(obj.unitPrice));
            tv_weight.setText(NumberHelp. formatMoney(obj.weight, 3, "--"));
            tv_totalPrice.setText(SteelardHelp.formatMoney(obj.calPrice()));

            setTag(obj);
            return null;
        }


    }

    public void onAddItem(OrderEntity.GoodEntity data) {
        adapter.notifyDataSetChanged();
    }

    public void onRemoveItem(int index, OrderEntity.GoodEntity data) {
        adapter.notifyDataSetChanged();
    }
    public void onRemoveAllItem() {
        adapter.notifyDataSetChanged();
    }


}
